﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace Web.BaseCode
{
    public class SiteMapProvider
    {
        string fileFullPath = string.Empty;
        XmlDocument xmlDoc = null;

        public SiteMapProvider(bool isDel = false)
        {
            fileFullPath = string.Format("{0}{1}", System.AppDomain.CurrentDomain.BaseDirectory, "sitemap.xml");
            xmlDoc = new XmlDocument();
            InitSiteMap(isDel);
        }

        /// <summary>
        /// 初始化sitemap的XmlDocument对象
        /// </summary>
        public void InitSiteMap(bool isDel)
        {
            if (!System.IO.File.Exists(fileFullPath))
            {
                XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xmlDoc.AppendChild(dec);

                XmlElement root = xmlDoc.CreateElement("urlset");
                root.SetAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
                xmlDoc.AppendChild(root);
            }
            else
            {
                if (isDel)
                {
                    System.IO.File.Delete(fileFullPath);
                    InitSiteMap(false);
                }
                else
                    xmlDoc.Load(fileFullPath);
            }
        }

        /// <summary>
        /// 根据页面类别添加url
        /// </summary>
        /// <param name="ename"></param>
        /// <param name="pageType"></param>
        public void AddUrl(BlogPageType pageType, string ename, DateTime updateTime)
        {
            //changefreq 值列表：always,hourly,daily,weekly,monthly,yearly,never
            //priorit 介于0.0-1.0之间，1.0最高

            XmlNode urlNode = xmlDoc.CreateElement("url");

            XmlElement loc = xmlDoc.CreateElement("loc");
            loc.InnerText = BaseCode.CommMethod.GetUrl(pageType, ename);
            urlNode.AppendChild(loc);

            XmlElement lastmod = xmlDoc.CreateElement("lastmod");
            lastmod.InnerText = updateTime.ToString("yyyy-MM-dd");
            urlNode.AppendChild(lastmod);

            XmlElement changefreq = xmlDoc.CreateElement("changefreq");
            changefreq.InnerText = this.GetChangefreq(pageType);
            urlNode.AppendChild(changefreq);

            XmlElement priority = xmlDoc.CreateElement("priority");
            priority.InnerText = this.GetPriority(pageType);
            urlNode.AppendChild(priority);

            xmlDoc.DocumentElement.AppendChild(urlNode);
            xmlDoc.Save(fileFullPath);
        }

        /// <summary>
        /// 更新一个url节点
        /// </summary>
        /// <param name="oldUrl"></param>
        /// <param name="newUrl"></param>
        public void UpdateUrl(string oldUrl, string newUrl)
        {
            for (int i = 0; i < xmlDoc.DocumentElement.ChildNodes.Count; i++)
            {
                XmlNode url = xmlDoc.DocumentElement.ChildNodes[i];
                for (int j = 0; j < url.ChildNodes.Count; j++)
                {
                    if (url.ChildNodes[j].Name.Equals("loc") && url.ChildNodes[j].InnerText.Equals(oldUrl))
                    {
                        url.ChildNodes[j].InnerText = newUrl;
                        url.ChildNodes[j + 1].InnerText = DateTime.Now.ToString("yyyy-MM-dd");
                        xmlDoc.Save(fileFullPath);
                        break;
                    }

                }
            }
        }

        /// <summary>
        /// 删除一个url节点
        /// </summary>
        /// <param name="ordUrl"></param>
        public void DeleteUrl(string oldUrl)
        {
            for (int i = 0; i < xmlDoc.DocumentElement.ChildNodes.Count; i++)
            {
                XmlNode url = xmlDoc.DocumentElement.ChildNodes[i];
                for (int j = 0; j < url.ChildNodes.Count; j++)
                {
                    if (url.ChildNodes[j].Name.Equals("loc") && url.ChildNodes[j].InnerText.Equals(oldUrl))
                    {
                        xmlDoc.DocumentElement.RemoveChild(url);
                        xmlDoc.Save(this.fileFullPath);
                    }
                }
            }
        }

        private string GetChangefreq(BlogPageType pageType)
        {
            string changefreqStr = string.Empty;

            switch (pageType)
            {
                case BlogPageType.Article:
                    {
                        changefreqStr = "monthly";
                        break;
                    }
                case BlogPageType.Category:
                    {
                        changefreqStr = "weekly";
                        break;
                    }
                case BlogPageType.Home:
                    {
                        changefreqStr = "always";
                        break;
                    }
                case BlogPageType.Item:
                    {
                        changefreqStr = "always";
                        break;
                    }
                //case BlogPageType.Search:
                //    {
                //        changefreqStr = "yearly";
                //        priorityStr = "0.8"; break;
                //    }
                case BlogPageType.Tag:
                    {
                        changefreqStr = "monthly";
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            return changefreqStr;
        }

        private string GetPriority(BlogPageType pageType)
        {
            string priorityStr = string.Empty;

            switch (pageType)
            {
                case BlogPageType.Article:
                    {
                        priorityStr = "0.8";
                        break;
                    }
                case BlogPageType.Category:
                    {
                        priorityStr = "0.7";
                        break;
                    }
                case BlogPageType.Home:
                    {
                        priorityStr = "1.0"; break;
                    }
                case BlogPageType.Item:
                    {
                        priorityStr = "0.9"; break;
                    }
                //case BlogPageType.Search:
                //    {
                //        changefreqStr = "yearly";
                //        priorityStr = "0.8"; break;
                //    }
                case BlogPageType.Tag:
                    {
                        priorityStr = "0.6"; break;
                    }
                default:
                    {
                        break;
                    }
            }
            return priorityStr;
        }
    }
}